
#include "bCharacter.h"
#include "..\Source\gsys\Gsys.h"
#include "System.h"
#include "osf.h"

extern hgeTTF::FontManager *FontMgr;
extern hgeTTF::Font* g_pFont  ;
extern std::vector<hge_ddx::SprManager_S> SprManager;
extern HTEXTURE* texList;


BOOL Character::InitCharacter( INT _heapsize, char* pfilename, INT characterWidth /*= 24*/, INT characterHeigh /*= 24*/,
	INT characterweight/*=400*/, INT characterColor /*= 0xff000000*/, BOOL bIsVerticalModel/*=FALSE*/ )
{

	FontMgr = new hgeTTF::FontManager;
	if (!FontMgr->Initialize())
	{
		gm_printf("FontMgr.Initialize() faile\n");
		return FALSE;
	}

	if (!FontMgr->loadFont(pfilename))
	{
		gm_printf("FontMgr.loadFont() faile\n");
		return FALSE;
	}

	g_pFont = FontMgr->getFont(pfilename);
	if (!g_pFont->cacheChars('a', 'z'))
	{
		gm_printf("g_pFont->cacheChars('a', 'z') faile\n");
		return FALSE;
	}

	if (!g_pFont->cacheChars(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
	{
		gm_printf("g_pFont->cacheChars('ABCDEFGHIJKLMNOPQRSTUVWXYZ') faile\n");
		return FALSE;
	}
	_charactersize_w = characterWidth;
	_charactersize_h = characterHeigh;
	_charactercolor = characterColor;

	return TRUE;
}
BOOL Character::InitCharacter(HANDLE hFontHeap, INT _heapsize, char* pfilename, INT characterWidth /*= 24*/, INT characterHeigh /*= 24*/,
	INT characterweight/*=400*/, INT characterColor /*= 0xff000000*/, BOOL bIsVerticalModel/*=FALSE*/ )
{
	return InitCharacter(-1,pfilename,characterWidth,characterHeigh,characterweight,characterColor,bIsVerticalModel);
}
BOOL Character::InitCharacter(HANDLE hFontHeap, char* pfilename, INT characterWidth /*= 24*/, INT characterHeigh /*= 24*/, INT characterweight/*=400*/, INT characterColor /*= 0xff000000*/, BOOL bIsVerticalModel/*=FALSE*/ )
{
	return InitCharacter(-1,pfilename,characterWidth,characterHeigh,characterweight,characterColor,bIsVerticalModel);
}
BOOL Character::SetCharacterColor( INT characterColor /*= 0xff000000*/ )
{
	_charactercolor = characterColor;
	return TRUE;
}

BOOL Character::SetCharacterSize( INT characterWidth /*= 24*/, INT characterHeigh /*= 24*/ )
{
	_charactersize_w = characterWidth;
	_charactersize_h = characterHeigh;
	return TRUE;
}

BOOL Character::SetCharacterDistance( INT widthdistance/*=0*/, INT heighdistance/*=0*/ )
{
	return TRUE;
}

BOOL Character::ChanageFont( char* pfilename, INT characterWidth /*= 24*/, INT characterHeigh /*= 24*/, INT characterColor /*= 0xff000000*/ )
{
	return TRUE;
}

HSPRITE Character::ConversionCharacter( TCHAR* pString, INT stringlenght ,DWORD dwPrioity, LPOINT pt, HGAMELAYER hspritelayer, SPRITE_FLAG_TYPE dwFlag /*= SF_BMPS_3D_LEFT*/ )
{
	//HSPRITE spr = new hgeSprite(0, 0,0,0,0);
	//SprManager_S temp;
	//temp.render = true;
	//temp.id = 0;
	//temp.tex = 0;
	//temp.spr = spr;
	//temp.priority = dwPrioity+1000*eLayUI;
	//temp._x = (FLOAT)pt.lx;
	//temp._y = (FLOAT)pt.ly;
	//temp.angle = 0;
	//temp.hscale = 1.0f;
	//temp.vscale=0;
	//LRECT_T r = {0,0,hge->System_GetState(HGE_SCREENWIDTH),hge->System_GetState(HGE_SCREENHEIGHT)};
	//temp.rect=r;
	//temp.usefont = true;
	//temp.fontsize = _charactersize_w;
	//temp.fontcolor = _charactercolor;
	//temp.fontsMaxWidth=0;

	//wcscpy( temp.wstr, pString);

	//SprManager.push_back(temp);
	//return spr;
	HSPRITE spr = new hgeSprite(0, 0,0,0,0);
	SprManager_S temp;
	temp.render = true;
	temp.id = 0;
	temp.tex = 0;
	temp.spr = spr;
	temp.priority = dwPrioity;
	temp._x = (FLOAT)pt.lx;
	temp._y = (FLOAT)pt.ly;
	temp.angle = 0;
	temp.hscale = 1.0f;
	temp.vscale=0;
	LRECT_T r = {0,0,hge->System_GetState(HGE_SCREENWIDTH),hge->System_GetState(HGE_SCREENHEIGHT)};
	temp.rect=r;
	temp.usefont = true;
	temp.fontsize = _charactersize_w;
	temp.fontcolor = _charactercolor;
	temp.spr->SetColor(_charactercolor);
	temp.fontsMaxWidth=0;
	wcscpy( temp.wstr, pString);
	if ((int)wcslen(pString)>stringlenght)
	{
		temp.wstr[stringlenght]=0;
	}
	SprManager.push_back(temp);
	return spr;
}

HSPRITE Character::BUF_ConversionCharacter(TCHAR * pString, pBUFFER_SPR_INFO pbufferinfo, INT * stringlenght, DWORD dwPrioity, LPOINT pt, HGAMELAYER hspritelayer, SPRITE_FLAG_TYPE dwFlag)
{
	HSPRITE spr = new hgeSprite(0, 0,0,0,0);
	SprManager_S temp;
	temp.render = true;
	temp.id = 0;
	temp.tex = 0;
	temp.spr = spr;
	temp.priority = dwPrioity;
	temp._x = (FLOAT)pt.lx;
	temp._y = (FLOAT)pt.ly;
	temp.angle = 0;
	temp.hscale = 1.0f;
	temp.vscale=0;
	LRECT_T r = {0,0,hge->System_GetState(HGE_SCREENWIDTH),hge->System_GetState(HGE_SCREENHEIGHT)};
	temp.rect=r;
	temp.usefont = true;
	temp.fontsize = _charactersize_w;
	temp.fontcolor = _charactercolor;
	temp.fontsMaxWidth=0;
	if (pbufferinfo->bIsSetRect)
	{
		temp.fontsMaxWidth = pbufferinfo->rectwidth;
	}
	wcscpy( temp.wstr, pString);
	*stringlenght = CalculateMaxFonts(  _charactersize_w,pString,temp.fontsMaxWidth);
	SprManager.push_back(temp);
	return spr;
}

INT Character::SF_UTF8toUincode( BYTE *pui8ptrUtf8,BYTE *pui8ptrUnicod )
{
#ifdef WIN32
	INT nu=0;
	if((char*)pui8ptrUtf8)

	{

		nu = strlen((char*)pui8ptrUtf8);

		MultiByteToWideChar(CP_ACP,0,(const char *)pui8ptrUtf8,(int)nu,(wchar_t*)pui8ptrUnicod,(int)nu);

		((wchar_t*)pui8ptrUnicod)[nu]=0;

	}
	return nu;
#endif
	INT	dwputlen=0;

	if(*pui8ptrUtf8==0x00)
	{
		*pui8ptrUnicod=0x00;
		*(pui8ptrUnicod+1)=0x00;
		return dwputlen;
	}
	while(*pui8ptrUtf8)
	{
		if(*pui8ptrUtf8>=0xE0)
		{
			*pui8ptrUnicod=((*(pui8ptrUtf8+1)<<6) & 0xC0) + ((*(pui8ptrUtf8+2)) & 0x3F);
			*(pui8ptrUnicod+1)=((*pui8ptrUtf8<<4) & 0xF0) +( (*(pui8ptrUtf8+1)>>2) & 0x0F);
			pui8ptrUtf8+=3;
			pui8ptrUnicod+=2;
			dwputlen+=2;
		}
		else if(*pui8ptrUtf8>=0xC0)
		{
			*pui8ptrUnicod=((*pui8ptrUtf8<<6) & 0xC0)+((*(pui8ptrUtf8+1)<<2) & 0x30) + ((*(pui8ptrUtf8+1)) & 0x0F);
			*(pui8ptrUnicod+1)=(*pui8ptrUtf8>>2) & 0x07 ;
			pui8ptrUtf8+=2;
			pui8ptrUnicod+=2;
			dwputlen+=2;
		}
		else if(*pui8ptrUtf8<0x80)
		{
			*pui8ptrUnicod=*pui8ptrUtf8;
			*(pui8ptrUnicod+1)=0x00;
			pui8ptrUnicod+=2;
			pui8ptrUtf8++;
			dwputlen+=2;
		}
		else
		{
			pui8ptrUtf8++;
		}
	}

	*pui8ptrUnicod=0x00;
	*(pui8ptrUnicod+1)=0x00;
	return dwputlen;
}


void Character::ReleaseCharacter()
{
	delete FontMgr;
}
//Character*	Character::GetInstance()
//{
//	if(_pcharacter == NULL)
//	{
//		_pcharacter = new	Character	;
//	}
//	return _pcharacter	;
//}

void Character::ClearcharacterBuffer()
{

}
void Character::ReInit()
{
	////memset(_pspritebuffer, 0, m_iBufferSize-1024);
	//
	//_pcurrentbufferstation = _pspritebuffer	;
	//_remainbuffersize = m_iBufferSize-100*1024	;
	////LogDebug("_pcurrentbufferstation=%d",_pcurrentbufferstation)
}

Character* g_pCharacter=new Character();



